package com.computergames.server.util;
import com.computergames.server.domain.CpuInfo;
import com.computergames.server.domain.MemoryInfo;
import com.computergames.server.domain.SizeEnum;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
import oshi.software.os.OperatingSystem;
import oshi.util.Util;
import oshi.hardware.CentralProcessor.TickType;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Properties;

public class HardWareUtils {

    //private HardWareUtil() {}

    /**
     * 等待休眠时间，单位ms
     */
    private static final int WAIT_TIME_MS = 1000;

    /**
     * 获取CPU信息
     *
     * @return CpuInfo对象，包含CPU数量及各种CPU时间
     *
     * 本方法通过oshi库获取系统CPU信息，计算各种CPU时间，包括系统时间、用户时间、等待时间、空闲时间等
     * 这些信息用于了解CPU的使用情况和系统负载
     */
    public static CpuInfo getCpuInfo() {
        // 初始化系统信息对象
        oshi.SystemInfo si = new oshi.SystemInfo();
        // 获取中央处理器对象
        CentralProcessor processor = si.getHardware().getProcessor();

        // 获取上一次CPU负载信息
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        // 短暂休眠，以便后续获取新的CPU负载信息
        Util.sleep(WAIT_TIME_MS);
        // 获取当前CPU负载信息
        long[] ticks = processor.getSystemCpuLoadTicks();

        // 计算各种CPU时间差值
        long nice = ticks[TickType.NICE.getIndex()] - prevTicks[TickType.NICE.getIndex()];
        long irq = ticks[TickType.IRQ.getIndex()] - prevTicks[TickType.IRQ.getIndex()];
        long softIrq = ticks[TickType.SOFTIRQ.getIndex()] - prevTicks[TickType.SOFTIRQ.getIndex()];
        long steal = ticks[TickType.STEAL.getIndex()] - prevTicks[TickType.STEAL.getIndex()];
        long cSys = ticks[TickType.SYSTEM.getIndex()] - prevTicks[TickType.SYSTEM.getIndex()];
        long user = ticks[TickType.USER.getIndex()] - prevTicks[TickType.USER.getIndex()];
        long ioWait = ticks[TickType.IOWAIT.getIndex()] - prevTicks[TickType.IOWAIT.getIndex()];
        long idle = ticks[TickType.IDLE.getIndex()] - prevTicks[TickType.IDLE.getIndex()];

        // 计算总的CPU时间
        long totalCpu = user + nice + cSys + idle + ioWait + irq + softIrq + steal;

        // 创建并填充CPU信息对象
        CpuInfo cpuInfo = new CpuInfo();
        cpuInfo.setCpuNum(processor.getLogicalProcessorCount());
        cpuInfo.setTotal(totalCpu);
        cpuInfo.setSys(cSys);
        cpuInfo.setUsed(user);
        cpuInfo.setWait(ioWait);
        cpuInfo.setFree(idle);

        // 返回CPU信息对象
        return cpuInfo;
    }


    /**
     * 获取内存信息
     *
     * @param size  大小单位，默认为B
     * @return      内存信息
     */
    public static MemoryInfo getMemoryInfo(SizeEnum size) {
        SystemInfo si = new SystemInfo();
        GlobalMemory memory = si.getHardware().getMemory();
        // 内存信息
        MemoryInfo mem = new MemoryInfo();
        mem.setTotal(Objects.isNull(size) ? memory.getTotal() : (float) memory.getTotal() / size.getSize());
        mem.setUsed(Objects.isNull(size) ? (memory.getTotal() - memory.getAvailable()) : (float) (memory.getTotal() - memory.getAvailable()) / size.getSize());
        mem.setFree(Objects.isNull(size) ? memory.getAvailable() : (float) memory.getAvailable() / size.getSize());
        return mem;
    }
}
